#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<fstream>
using namespace std;
ofstream text;

int dightset()
{
	int B;
	cout<<"请输入数据范围X，计算数据将控制在0到X的范围内,X不大于10000。"<<endl;
	while(1)
	{
     	cin>>B;
		if(B>10000||B<=0)
			cout<<"数据错误，请重新输入。"<<endl;
		else
			cout<<"设置成功。"<<endl;
			break;
	}
	return B;
}

int operset()
{
	int C;
	cout<<"请输入定制运算类型："<<endl<<"1.加法运算。"<<endl<<"2.减法运算。"<<endl<<"3.乘法运算。"<<endl<<"4.除法运算。"<<endl<<"5.随机运算。"<<endl;
	while(1)
	{
     	cin>>C;
		if(C>5||C<=0)
			cout<<"数据错误，请重新输入。"<<endl;
		else 
			cout<<"设置成功。"<<endl;
			break;
	}
	return C;
}

int keyset()
{
	int D;
    cout<<"请输入定制结果类型："<<endl<<"1.允许出现负数。"<<endl<<"2.必须除尽。"<<endl<<"3.结果为10以内。"<<endl<<"4.结果为100以内。"<<endl<<"5.结果为1000以内。"<<endl;
		while(1)
	{
     	cin>>D;
		if(D>3||D<=0)
			cout<<"数据错误，请重新输入。"<<endl;
		else 
			cout<<"设置成功。"<<endl;
			break;
	}
	return D;
}

int formatset()
{
	int E;
    cout<<"请输入除法结果保留小数位数（0 到3）"<<endl;
		while(1)
	{
     	cin>>E;
		if(E>3||E<=0)
			cout<<"数据错误，请重新输入。"<<endl;
		else 
			cout<<"设置成功。"<<endl;
			break;
	}
	return E;
}

int numset()
{
	int F;
    cout<<"请输入题目数量,大于0且不超过2000。"<<endl;
	while(1)
	{
     	cin>>F;
		if(F>2000||F<=0)
			cout<<"数据错误，请重新输入。"<<endl;
		else 
			cout<<"设置成功。"<<endl;
			break;
	}
	return F;
}

int main()
{
	text.open("C:\\Users\\14915\\Desktop\\课设\\课设\\题目.txt", ios::out);
	int function=0,dight=100,oper=5,key=1,format=2,num=5;
	char C;
	srand((unsigned)time(NULL));
	//随机数发生器的初始化
	cout<<"四则运算题目生成系统"<<endl;
	cout<<"默认设置为100以内的5道随机运算。"<<endl;
	while(function!=6)
	{
	cout<<"请选择题目定制功能。"<<endl<<"1.选择计算范围。"<<endl<<"2.定制运算类型。"<<endl<<"3.定制结果类型。"<<endl<<"4.定制输出形式。"<<endl<<"5.设置题目数量。"<<endl<<"6.设置完毕，开始生成题目。"<<endl;
	cin>>function;
	switch(function)
	{
	    case 1:dight=dightset();break;
		case 2:oper=operset();break;
		case 3:key=keyset();break;
		case 4:format=formatset();break;
        case 5:num=numset();break;
	}}
	int A[2048],B[2048],b[2048],i,t=0,k,T=0,limit=1000000,jud;
	double a[2048],c[2048];
	if (key==3)
		limit=10;
	else if(key==4)
		limit=100;
	else if(key==5)
		limit=1000;
	for(i = 0;i<num;i++)
    {
	//生成随机符号
	if(oper==5)
	k = rand()%4;
	else k=oper;
		if(k == 1)
		  C = '+';
	    else if(k == 2)
		  C = '-';
	    else if(k == 3)
		  C = '*';
   	    else if(k == 0)
	      C = '/';

	//输出题目
    if(C == '+')
    {
		while(1)
		{
		A[i] = rand()%dight;
		B[i] = rand()%dight;
		a[i] = A[i]+B[i];
		if(a[i]>limit);
		else break;
		}
		cout<<A[i]<<"+"<<B[i]<<"=?"<<endl;
		text<<A[i]<<"+"<<B[i]<<"=?"<<endl;
	}
    else if(C == '-')
    {
		//结果能否为负数
		while(1)
		{
		A[i] = rand()%dight;
		B[i] = rand()%dight;
		a[i] = A[i]-B[i];
		if(a[i]>limit);
	    else if(a[i]>0||key==1)
			break;
		}
		cout<<A[i]<<"-"<<B[i]<<"=?"<<endl;
		text<<A[i]<<"-"<<B[i]<<"=?"<<endl;
	}
    else if(C == '*')
    {
		while(1)
		{
		A[i] = rand()%dight;
		B[i] = rand()%dight;
		a[i] = A[i]*B[i];
		if(a[i]>limit);
		else break;
		}
		cout<<A[i]<<"*"<<B[i]<<"=?"<<endl;
		text<<A[i]<<"*"<<B[i]<<"=?"<<endl;
	}
    else if(C == '/')
    {
		//结果能否除尽,并且除数不能为0
		while(1)
		{
		A[i] = rand()%dight;
		B[i] = rand()%dight;
		a[i] = 1.0*A[i]/B[i];
		if(a[i]>limit||B[i]==0);
	    else if(A[i]%B[i]==0||key!=2)
			break;
		}
		cout<<A[i]<<"/"<<B[i]<<"=?"<<endl;
		text<<A[i]<<"/"<<B[i]<<"=?"<<endl;
	}
    }
	//输入结果
    cout<<"结果为：";
    for(i=0;i<num;i++)
    {
        cin>>b[i];
        if((int)a[i] == b[i])
            t++;
		//正确题计数
        else
        {
            c[T] = i;
			//错题计数
            T++;
        }
    }

    if(t==num-1)
        cout<<"恭喜你都答对了！"<<endl;
    else
    {
        cout<<"第";
        for(i=0;i<T;i++)
            cout<<c[i]+1<<"题错了！"<<endl;
    }
	
	while(1)
	{
		cout<<"按1输出答案。"<<endl<<"按2写入文件。"<<endl<<"按3结束程序。"<<endl;
     	cin>>jud;
		if(jud==1)
		{
			 for(i=0;i<num;i++)
				{ 
					if(format==0)
				        printf("第%d题的答案为%.0f\n",i+1,a[i]);
					else if(format==1)
						printf("第%d题的答案为%.1f\n",i+1,a[i]);
					else if(format==2)
						printf("第%d题的答案为%.2f\n",i+1,a[i]);
					else if(format==3)
						printf("第%d题的答案为%.3f\n",i+1,a[i]);
			 }}
		else if(jud==2)
			for(i=0;i<num;i++)
				{ 
					text<<"第"<<i+1<<"题的答案为"<<a[i]<<endl;
			}
		else if(jud==3)
		break;
		else cout<<"输入错误，请重新输入。"<<endl;
	}
	return 1;
}
